package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lcw.library.imagepicker.ImagePicker;
import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseInfoBean;
import com.zlx.module_base.base_api.res_data.NatureBean;
import com.zlx.module_base.base_api.res_data.OccupationBean;
import com.zlx.module_base.base_api.res_data.UpdateUserBean;
import com.zlx.module_base.base_api.res_data.UpdateUserPostBean;
import com.zlx.module_base.base_api.res_data.UploadBean;
import com.zlx.module_base.base_api.res_data.VideoChannel;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.bean.InfoSex;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.GlideLoader;
import com.zlx.sharelive.utils.StringUt;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 个人资料
 */
public class MyInfoAc extends BaseAc {

    @BindView(R.id.iv_my_info_head)
    ImageView iv_my_info_head;
    @BindView(R.id.tv_info_nickname)
    TextView tv_info_nickname;
    @BindView(R.id.tv_info_id)
    TextView tv_info_id;
    @BindView(R.id.tv_info_sex)
    TextView tv_info_sex;
    @BindView(R.id.tv_info_age)
    TextView tv_info_age;
    @BindView(R.id.tv_info_type)
    TextView tv_info_type;
    @BindView(R.id.tv_info_occupation)
    TextView tv_info_occupation;
    @BindView(R.id.tv_info_autograph)
    TextView tv_info_autograph;
    @BindView(R.id.tv_info_preference)
    TextView tv_info_preference;

    private String sex;
    private String birthday;
    private Date birthdayDate;
    private String age;
    private String avatar;
    private String nickname;
    private String makingType;
    private String signature;
    private String occupation;
    private String videoLike;
    private BaseInfoBean mBaseInfoBean;
    private String filePath = "";
    private List<File> fileList = new ArrayList<>();
    private List<InfoSex> mInfoSexList = new ArrayList<>();
    private CommonRecyclerViewAdapter<InfoSex> mAdapter;
    private List<NatureBean.InfoDTO> mMakeFriendsList = new ArrayList<>();
    private CommonRecyclerViewAdapter<NatureBean.InfoDTO> mMakeFriendsAdapter;
    private List<OccupationBean.InfoDTO> mOccupationList = new ArrayList<>();
    private CommonRecyclerViewAdapter<OccupationBean.InfoDTO> mOccupationAdapter;
    private List<VideoChannel.InfoDTO> mVideoChanneList = new ArrayList<>();
    private CommonRecyclerViewAdapter<VideoChannel.InfoDTO> mVideoChanneAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.ac_my_info;
    }

    @Override
    public void initViews() {
        super.initViews();

        mBaseInfoBean = (BaseInfoBean) getIntent().getSerializableExtra("info");

        initSexInfoList();
        setInfoData();
    }

    private void initSexInfoList() {
        InfoSex infoSex = new InfoSex();
        infoSex.setName("男");
        infoSex.setSex("1");
        infoSex.setCheck(false);
        InfoSex infoSex2 = new InfoSex();
        infoSex2.setName("女");
        infoSex2.setSex("2");
        infoSex2.setCheck(false);
        mInfoSexList.add(infoSex);
        mInfoSexList.add(infoSex2);
    }

    /**
     * 回显数据
     */
    private void setInfoData() {
        BaseInfoBean.InfoDTO data = mBaseInfoBean.getInfo().get(0);
        if (!StringUt.isEmpty(data.getAvatar())) {
            GlideImgManager.glideLoader(this, data.getAvatar(), iv_my_info_head, 0);
        }
        try {
            sex = data.getSex() + "";
            birthday = data.getBirthday();
            age = data.getAge() + "";
            avatar = data.getAvatar();
            nickname = data.getNickname();
            makingType = data.getMakingType();
            signature = data.getSignature();
            occupation = data.getOccupation();
            videoLike = data.getVideoLike();
            tv_info_nickname.setText(data.getNickname());
            tv_info_id.setText(data.getId() + "");

            if (StringUt.isEmpty(data.getSex())) {
                tv_info_sex.setText("未知");
            } else {
                switch (data.getSex()) {
                    case "1":
                        tv_info_sex.setText("男");
                        break;
                    case "2":
                        tv_info_sex.setText("女");
                        break;
                    default:
                    case "0":
                        tv_info_sex.setText("未知");
                        break;
                }
            }
            if (!StringUt.isEmpty(data.getAge())) {
                tv_info_age.setText(data.getAge());
            } else {
                tv_info_age.setText("未知");
            }
            tv_info_type.setText(data.getMakingType());
            tv_info_occupation.setText(data.getOccupation());
            tv_info_autograph.setText(data.getSignature());
            tv_info_preference.setText(data.getVideoLike());
        } catch (Exception e) {

        }
    }

    @OnClick({R.id.iv_back, R.id.ll_head, R.id.ll_date, R.id.ll_sex, R.id.ll_nickname
            , R.id.ll_autograph, R.id.ll_make_friends, R.id.ll_occupation, R.id.ll_video_channel})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_head:
                //修改头像
                ImagePicker.getInstance()
                        .setTitle("选择头像")//设置标题
                        .showCamera(true)//设置是否显示拍照按钮
                        .showImage(true)//设置是否展示图片
                        .showVideo(false)//设置是否展示视频
                        .setSingleType(true)//设置图片视频不能同时选择
                        .setMaxCount(1)//设置最大选择图片数目(默认为1，单选)
                        .setImageLoader(new GlideLoader())//设置自定义图片加载器
                        .start(MyInfoAc.this, 1);//REQEST_SELECT_IMAGES_CODE为Intent调用的requestCode      break;
                break;
            case R.id.ll_date:
                //设置年龄
                DatePicker picker = new DatePicker(this, DatePicker.YEAR_MONTH_DAY);
                picker.setRange(1950, 2050);//年份范围
                picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
                    @Override
                    public void onDatePicked(String year, String month, String day) {
                        Calendar calendar = Calendar.getInstance();
                        int year1 = calendar.get(Calendar.YEAR);
                        age = String.valueOf(year1 - Integer.valueOf(year));
                        tv_info_age.setText(age);
                        birthday = year + "-" + month + "-" + day;
                        birthdayDate = getDate(year + "-" + month + "-" + day);
                        updateInfo();
                    }
                });
                picker.show();
                break;
            case R.id.ll_sex:
                //修改性别
                if (null != mBaseInfoBean) {
                    if (null != mBaseInfoBean.getInfo()) {
                        if (mBaseInfoBean.getInfo().size() > 0) {
                            if (!StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getSex())) {
                                Toast.makeText(MyInfoAc.this, "性别不可修改", Toast.LENGTH_SHORT).show();
                            } else {
                                dialogSex();
                            }
                        }
                    }
                }

                break;
            case R.id.ll_nickname:
                //修改昵称
                startActivityForResult(new Intent(this, NickNameAc.class)
                        .putExtra("nickname", tv_info_nickname.getText().toString().trim()), 20);
                break;
            case R.id.ll_autograph:
                //修改签名
                startActivityForResult(new Intent(this, AutographAc.class)
                        .putExtra("autograph", tv_info_autograph.getText().toString().trim()), 20);
                break;
            case R.id.ll_make_friends:
                //交友属性
                getNatureList();
                break;
            case R.id.ll_occupation:
                //职业列表
                getOccupationList();
                break;
            case R.id.ll_video_channel:
                //观影偏好
                getVideoChannel();
                break;
        }
    }

    /**
     * 频道列表
     */
    private void getVideoChannel() {
        ApiUtil.getVideoApi().getVideoChannel(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<VideoChannel>>() {
            @Override
            public void onSuccess(ApiResponse<VideoChannel> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
                                    mVideoChanneList.clear();
                                    mVideoChanneList.addAll(data.getData().getInfo());

                                    //回显
                                    if (null != mBaseInfoBean) {
                                        if (null != mBaseInfoBean.getInfo()) {
                                            if (mBaseInfoBean.getInfo().size() > 0) {
                                                BaseInfoBean.InfoDTO mData = mBaseInfoBean.getInfo().get(0);
                                                String mVideoLike = mData.getVideoLike();
                                                if (!StringUt.isEmpty(mVideoLike)) {
                                                    String[] videoList = mVideoLike.split(",");
                                                    if (videoList.length > 0) {
                                                        if (mVideoChanneList.size() > 0) {
                                                            for (int i = 0; i < mVideoChanneList.size(); i++) {
                                                                for (int j = 0; j < videoList.length; j++) {
                                                                    if (mVideoChanneList.get(i).getName().equals(videoList[j])) {
                                                                        //相同 存在
                                                                        mVideoChanneList.get(i).setCheck(true);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    dialogVideoChannel();
                                } else {
                                    Toast.makeText(MyInfoAc.this, "暂无数据", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
            }
        }));
    }

    private void dialogVideoChannel() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_make_friends_list2, null);
        dialog.setContentView(view);

        RecyclerView dialog_recycler = view.findViewById(R.id.dialog_recycler);

        TextView tv_type = view.findViewById(R.id.tv_type);

        tv_type.setText("观影偏好");

        dialog_recycler.setLayoutManager(new GridLayoutManager(this, 3));

        mVideoChanneAdapter = new CommonRecyclerViewAdapter<VideoChannel.InfoDTO>(this, mVideoChanneList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, VideoChannel.InfoDTO entity, int position) {
                holder.setText(R.id.tv_text, entity.getName());
                TextView tv_text = holder.getView(R.id.tv_text);
                if (entity.isCheck() == true) {
                    tv_text.setTextColor(getResources().getColor(R.color.white));
                    tv_text.setBackgroundResource(R.drawable.button_bg_hot_pink);
                } else {
                    tv_text.setTextColor(getResources().getColor(R.color.dove_gray));
                    tv_text.setBackgroundResource(R.drawable.edit_bg_gallery);
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_channel_list2;
            }
        };

        dialog_recycler.setAdapter(mVideoChanneAdapter);

        TextView tv_complete = view.findViewById(R.id.tv_complete);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        tv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoLike = "";
                List<VideoChannel.InfoDTO> mNewList = new ArrayList<>();
                for (int i = 0; i < mVideoChanneList.size(); i++) {
                    if (mVideoChanneList.get(i).isCheck() == true) {
                        mNewList.add(mVideoChanneList.get(i));
                    }
                }
                for (int i = 0; i < mNewList.size(); i++) {
                    if (i < (mNewList.size() - 1)) {
                        videoLike += mNewList.get(i).getName() + ",";
                    } else {
                        videoLike += mNewList.get(i).getName();
                    }
                }
                tv_info_preference.setText(videoLike);
                updateInfo();
                dialog.dismiss();
            }
        });

        mVideoChanneAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (mVideoChanneList.get(position).isCheck() == true) {
                    mVideoChanneList.get(position).setCheck(false);
                } else {
                    mVideoChanneList.get(position).setCheck(true);
                }
                mVideoChanneAdapter.notifyDataSetChanged();
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    /**
     * 职业列表
     */
    private void getOccupationList() {
        ApiUtil.getUserApi().getOccupationList(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<OccupationBean>>() {
            @Override
            public void onSuccess(ApiResponse<OccupationBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
                                    mOccupationList.clear();
                                    mOccupationList.addAll(data.getData().getInfo());
                                    if (mOccupationList.size() > 0) {
                                        for (int i = 0; i < mOccupationList.size(); i++) {
                                            if (i == 0) {
                                                mOccupationList.get(0).setCheck(true);
                                            } else {
                                                mOccupationList.get(i).setCheck(false);
                                            }
                                        }
                                    }
                                    dialogOccupation();
                                } else {
                                    Toast.makeText(MyInfoAc.this, "暂无数据", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
            }
        }));
    }

    private void dialogOccupation() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_make_friends_list, null);
        dialog.setContentView(view);

        TextView tv_type = view.findViewById(R.id.tv_type);
        tv_type.setText(getResources().getString(R.string.job_occupation));

        RecyclerView dialog_recycler = view.findViewById(R.id.dialog_recycler);

        dialog_recycler.setLayoutManager(new LinearLayoutManager(this));

        mOccupationAdapter = new CommonRecyclerViewAdapter<OccupationBean.InfoDTO>(this, mOccupationList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, OccupationBean.InfoDTO entity, int position) {
                holder.setText(R.id.tv_text, entity.getName());
                TextView tv_text = holder.getView(R.id.tv_text);
                if (entity.isCheck() == true) {
                    tv_text.setTextColor(getResources().getColor(R.color.black));
                } else {
                    tv_text.setTextColor(getResources().getColor(R.color.martini));
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_channel_list;
            }
        };

        dialog_recycler.setAdapter(mOccupationAdapter);

        ImageView iv_close = view.findViewById(R.id.iv_close);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        TextView tv_complete = view.findViewById(R.id.tv_complete);

        tv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < mOccupationList.size(); i++) {
                    if (mOccupationList.get(i).isCheck() == true) {
                        tv_info_occupation.setText(mOccupationList.get(i).getName());
                        occupation = mOccupationList.get(i).getName();
                    }
                }
                updateInfo();
                dialog.dismiss();
            }
        });

        mOccupationAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                for (int i = 0; i < mOccupationList.size(); i++) {
                    if (position == i) {
                        mOccupationList.get(position).setCheck(true);
                    } else {
                        mOccupationList.get(i).setCheck(false);
                    }
                }
                mOccupationAdapter.notifyDataSetChanged();
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    /**
     * 交友属性列表
     */
    private void getNatureList() {
        ApiUtil.getUserApi().getNatureList(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<NatureBean>>() {
            @Override
            public void onSuccess(ApiResponse<NatureBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
                                    mMakeFriendsList.clear();
                                    mMakeFriendsList.addAll(data.getData().getInfo());
                                    if (mMakeFriendsList.size() > 0) {
                                        for (int i = 0; i < mMakeFriendsList.size(); i++) {
                                            if (i == 0) {
                                                mMakeFriendsList.get(0).setCheck(true);
                                            } else {
                                                mMakeFriendsList.get(i).setCheck(false);
                                            }
                                        }
                                    }
                                    dialogMakeFriends();
                                } else {
                                    Toast.makeText(MyInfoAc.this, "暂无数据", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
            }
        }));
    }

    private void dialogMakeFriends() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_make_friends_list, null);
        dialog.setContentView(view);

        RecyclerView dialog_recycler = view.findViewById(R.id.dialog_recycler);

        dialog_recycler.setLayoutManager(new LinearLayoutManager(this));

        mMakeFriendsAdapter = new CommonRecyclerViewAdapter<NatureBean.InfoDTO>(this, mMakeFriendsList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, NatureBean.InfoDTO entity, int position) {
                holder.setText(R.id.tv_text, entity.getName());
                TextView tv_text = holder.getView(R.id.tv_text);
                if (entity.isCheck() == true) {
                    tv_text.setTextColor(getResources().getColor(R.color.black));
                } else {
                    tv_text.setTextColor(getResources().getColor(R.color.martini));
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_channel_list;
            }
        };

        dialog_recycler.setAdapter(mMakeFriendsAdapter);

        ImageView iv_close = view.findViewById(R.id.iv_close);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        TextView tv_complete = view.findViewById(R.id.tv_complete);

        tv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < mMakeFriendsList.size(); i++) {
                    if (mMakeFriendsList.get(i).isCheck() == true) {
                        tv_info_type.setText(mMakeFriendsList.get(i).getName());
                        makingType = mMakeFriendsList.get(i).getName();
                    }
                }
                updateInfo();
                dialog.dismiss();
            }
        });

        mMakeFriendsAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                for (int i = 0; i < mMakeFriendsList.size(); i++) {
                    if (position == i) {
                        mMakeFriendsList.get(position).setCheck(true);
                    } else {
                        mMakeFriendsList.get(i).setCheck(false);
                    }
                }
                mMakeFriendsAdapter.notifyDataSetChanged();
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    private void dialogSex() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_channel_list, null);
        dialog.setContentView(view);

        RecyclerView dialog_recycler = view.findViewById(R.id.dialog_recycler);

        dialog_recycler.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<InfoSex>(this, mInfoSexList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, InfoSex entity, int position) {
                holder.setText(R.id.tv_text, entity.getName());
                TextView tv_text = holder.getView(R.id.tv_text);
                if (entity.isCheck() == true) {
                    tv_text.setTextColor(getResources().getColor(R.color.black));
                } else {
                    tv_text.setTextColor(getResources().getColor(R.color.martini));
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_channel_list;
            }
        };

        dialog_recycler.setAdapter(mAdapter);

        ImageView iv_close = view.findViewById(R.id.iv_close);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        TextView tv_complete = view.findViewById(R.id.tv_complete);

        tv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < mInfoSexList.size(); i++) {
                    if (mInfoSexList.get(i).isCheck() == true) {
                        tv_info_sex.setText(mInfoSexList.get(i).getName());
                        sex = mInfoSexList.get(i).getSex();
                    }
                }
                updateInfo();
                dialog.dismiss();
            }
        });

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                for (int i = 0; i < mInfoSexList.size(); i++) {
                    if (position == i) {
                        mInfoSexList.get(position).setCheck(true);
                    } else {
                        mInfoSexList.get(i).setCheck(false);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            List<String> imagePaths = data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
            filePath = imagePaths.get(0);
            GlideImgManager.glideLoaderFile(MyInfoAc.this, new File(imagePaths.get(0)), iv_my_info_head, 0);
            showLoading();
            fileList.clear();
            fileList.add(new File(filePath));
            updateImage();
        }
        if (requestCode == 20 && resultCode == 30) {
            tv_info_nickname.setText(data.getStringExtra("nickname"));
            nickname = data.getStringExtra("nickname");
            updateInfo();
        }
        if (requestCode == 20 && resultCode == 40) {
            tv_info_autograph.setText(data.getStringExtra("autograph"));
            signature = data.getStringExtra("autograph");
            updateInfo();
        }
    }

    private void updateInfo() {
        UpdateUserPostBean updateUserPostBean = new UpdateUserPostBean();
        updateUserPostBean.setSex(sex);
        updateUserPostBean.setBirthday(birthday);
        updateUserPostBean.setAge(age);
        updateUserPostBean.setAvatar(avatar);
        updateUserPostBean.setNickname(nickname);
        updateUserPostBean.setMakingType(makingType);
        updateUserPostBean.setSignature(signature);
        updateUserPostBean.setOccupation(occupation);
        updateUserPostBean.setVideoLike(videoLike);
        ApiUtil.getUserApi().updateUser(SPUtil.getToken(MyInfoAc.this),
                updateUserPostBean
        ).observe(MyInfoAc.this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UpdateUserBean>>() {
            @Override
            public void onSuccess(ApiResponse<UpdateUserBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            Toast.makeText(MyInfoAc.this, "修改成功", Toast.LENGTH_SHORT).show();
                            getBaseInfo();
                        } else {
                            Toast.makeText(MyInfoAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }));
    }

    private void updateImage() {
        ApiUtil.getVideoApi().upload(filesToMultipartBody(fileList)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UploadBean>>() {
            @Override
            public void onSuccess(ApiResponse<UploadBean> data) {
                if (null != data.getData()) {
                    if (data.getData().getCode() == 0) {
                        showSuccess();
                        avatar = data.getData().getInfo().get(0).getUrl();
                        updateInfo();
                    } else {
                        Toast.makeText(MyInfoAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    showSuccess();
                    Toast.makeText(MyInfoAc.this, "系统异常", Toast.LENGTH_SHORT).show();
                }
            }
        }));
    }

    public static MultipartBody filesToMultipartBody(List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();

        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }


    /**
     * 获取当前时间
     *
     * @return
     */
    public Date getDate(String str) {
        try {
            java.text.SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyy-MM-dd");
            Date date = formatter.parse(str);
            return date;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    /**
     * 用户基本信息
     */
    private void getBaseInfo() {
        ApiUtil.getUserApi().getBaseInfo(SPUtil.getToken(this)).
                observe(this, new BaseObserver<>(
                        new BaseObserverCallBack<ApiResponse<BaseInfoBean>>() {
                            @Override
                            public void onSuccess(ApiResponse<BaseInfoBean> data) {
                                if (null != data) {
                                    if (null != data.getData()) {
                                        if (data.getData().getCode() == 0) {
                                            if (null != data.getData().getInfo()) {
                                                mBaseInfoBean = data.getData();
                                            }
                                        }
                                    }
                                }
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();

                            }
                        }));
    }
}
